package com.pan.hisystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pan.hisystem.mapper.PatientMapper;
import com.pan.hisystem.pojo.*;
import com.pan.hisystem.mapper.OutpatientsMapper;
import com.pan.hisystem.service.IMedicalCardService;
import com.pan.hisystem.service.IOutpatientsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pan.hisystem.service.IPrescriptionService;
import com.pan.hisystem.service.IRegistrationService;
import com.pan.hisystem.utils.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author qingyang
 * @since 2022-03-18
 */
@Service
public class OutpatientsServiceImpl extends ServiceImpl<OutpatientsMapper, Outpatients> implements IOutpatientsService {

    @Autowired
    private OutpatientsMapper outpatientsMapper;
    @Autowired
    private IMedicalCardService medicalCardService;
    @Autowired
    private PatientMapper patientMapper;
    @Autowired
    private IPrescriptionService prescriptionService;
    @Autowired
    private IRegistrationService registrationService;

    /**
     * 根据卡号获取患者信息
     * @param mid
     * @param id
     * @return
     */
    @Override
    @Transactional
    public RespBean getMessageByMid(String mid, Integer id){
        //查询该门诊医生是否有该患者的挂号
        Registration registration=registrationService.getOne(new QueryWrapper<Registration>()
                .eq("uid",id)
                .eq("mid",mid)
                .eq("back_status",false)
                .eq("enabled",false));
        if(registration==null){
            return RespBean.error("暂无该患者的挂号!");
        }
        if(!registration.getPayStatus()){
            return RespBean.error("患者未缴挂号费");
        }
        Patient patientMes=patientMapper.getMessageByMid(mid);
        Outpatients outpatients=outpatientsMapper.selectOne(new QueryWrapper<Outpatients>()
                .eq("rid",registration.getId()));


        //直接生成对应的处方号,实现一个挂号对应一个就诊号对应一个处方号
        if(patientMes!=null){
            //避免出现无效处方号
            if(outpatients.getPreId()!=null){
                return RespBean.success("获取成功!",patientMes);
            }
            Prescription prescription=new Prescription();
            prescription.setId(IdUtils.getPrescriptionId());
            prescription.setCreateTime(LocalDateTime.now());
            prescription.setPayStatus(false);
            prescription.setTakeStatus(false);
            prescription.setBackStatus(false);
            outpatients.setPreId(prescription.getId());
            prescriptionService.save(prescription);
            outpatientsMapper.updateById(outpatients);
            return  RespBean.success("获取成功!",patientMes);
        }
        return RespBean.error("获取失败!");
    }

    /**
     * 更新就诊状态
     * @param outpatients
     * @return
     */
    @Override
    @Transactional
    public RespBean updateOutpatients(Outpatients outpatients) {
        Registration registration=registrationService.getOne(new QueryWrapper<Registration>().eq("id",outpatients.getRid()));
        if(outpatientsMapper.updateById(outpatients)==1){
            if(outpatients.getEnabled()){
                registration.setEnabled(true);
                outpatients.setFinallyTime(LocalDateTime.now());
                outpatientsMapper.updateById(outpatients);
                registrationService.updateById(registration);
                return RespBean.success("更新成功!");
            }

            return RespBean.success("更新成功!");

        }
        return RespBean.error("更新失败!");
    }

    /**
     * 根据卡号获取患者化验收费信息
     * @param mid
     * @return
     */
    @Override
    public List<Outpatients> getTestByMid(String mid) {
        return outpatientsMapper.getTestByMid(mid);
    }


}
